﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Text;

namespace BookDemo.S07.S702
{
    class S702 : IBookDemo
    {
        public void Main(string[] args)
        {
            var configuration = new ConfigurationBuilder()
                 .AddJsonFile("profile.json")
                 .Build();

            var serviceProvider = new ServiceCollection()
                .AddOptions()
                .Configure<Profile>("foo", configuration.GetSection("foo"))
                .Configure<Profile>("bar", configuration.GetSection("bar"))
                .BuildServiceProvider();

            var optionsAccessor = serviceProvider
                .GetRequiredService<IOptionsSnapshot<Profile>>();
            Print(optionsAccessor.Get("foo"));
            Print(optionsAccessor.Get("bar"));

            static void Print(Profile profile)
            {
                Console.WriteLine($"Gender: {profile.Gender}");
                Console.WriteLine($"Age: {profile.Age}");
                Console.WriteLine($"Email Address: {profile.ContactInfo.EmailAddress}");
                Console.WriteLine($"Phone No: {profile.ContactInfo.PhoneNo}\n");
            }
        }
        
    }
    public enum Gender
    {
        Male,
        Female
    }
    public class ContactInfo : IEquatable<ContactInfo>
    {
        public string EmailAddress { get; set; }
        public string PhoneNo { get; set; }
        public bool Equals(ContactInfo other)
        {
            return other == null
               ? false
               : EmailAddress == other.EmailAddress && PhoneNo == other.PhoneNo;
        }
    }
    public class Profile : IEquatable<Profile>
    {
        public Gender Gender { get; set; }
        public int Age { get; set; }
        public ContactInfo ContactInfo { get; set; }

        public Profile() { }
        public Profile(Gender gender, int age, string emailAddress, string phoneNo)
        {
            Gender = gender;
            Age = age;
            ContactInfo = new ContactInfo
            {
                EmailAddress = emailAddress,
                PhoneNo = phoneNo
            };
        }
        public bool Equals(Profile other)
        {
            return other == null
                ? false
                : Gender == other.Gender &&
                  Age == other.Age &&
                  ContactInfo.Equals(other.ContactInfo);
        }
    }
}
